package org.cqipc.edu.service.impl;

import org.cqipc.edu.common.Pages;
import org.cqipc.edu.common.Result;
import org.cqipc.edu.common.Status;
import org.cqipc.edu.dao.*;
import org.cqipc.edu.entity.Goods;
import org.cqipc.edu.entity.User;
import org.cqipc.edu.service.GoodsService;
import org.cqipc.edu.utils.FileUtils;
import org.cqipc.edu.vo.GoodsResultVO;
import org.cqipc.edu.vo.ResultGoodsVO;
import org.cqipc.edu.vo.ShowGoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 宠物接口实现
 * @ClassNmae: GoodsServiceImpl
 * @Author: HeYue
 * @DateTime: 2020-11-01 16:43
 **/
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;
    @Resource
    private Type1Dao type1Dao;
    @Resource
    private Type2Dao type2Dao;
    @Resource
    private Type3Dao type3Dao;
    @Resource
    private UserDao userDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addGoodsInfo(MultipartFile file, GoodsResultVO goodsResultVO, HttpSession session) throws IOException {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsResultVO, goods);
        String path = FileUtils.FileUpload(file);
        if (path.equals("error_size")) {
            return "error_size";
        }
        goods.setGoodsStatus(2)
                .setGoodsImage(path)
                .setUserId(((User) session.getAttribute("loginInfo")).getUserId());
        return goodsDao.insertGoods(goods) ? "success" : "error";
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getGoodsPage(Pages pages, HttpSession session) {
        Result<Goods> result = new Result<>();
        result.setData(goodsDao.queryGoodsByUserAndPage(pages.getPage(), pages.getLimit(),
                ((User) session.getAttribute("loginInfo")).getUserId()))
                .setCode(Status.SUCCESS)
                .setMsg("success")
                .setCount(goodsDao.queryGoodsByUserAll(((User) session.getAttribute("loginInfo")).getUserId()).size());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object batchGrounding(List<Integer> ids) {
        return goodsDao.batchUpdateGoodsStatus1(ids) ? "success" : "error";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object batchUndercarriage(List<Integer> ids) {
        return goodsDao.batchUpdateGoodsStatus2(ids) ? "success" : "error";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object batchDelete(List<Integer> ids) {
        return goodsDao.batchDelGoods(ids) ? "success" : "error";
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getGoodsById(Integer id) {
        Goods goods = goodsDao.queryById(id);
        ResultGoodsVO vo = new ResultGoodsVO();
        BeanUtils.copyProperties(goods, vo);
        vo.setType1Id(type1Dao.queryType1ById(goods.getType1Id()).getType1Name())
                .setType2Id(type2Dao.queryBytype2d(goods.getType2Id()).getType2Name())
                .setType3Id(type3Dao.queryBytype3d(goods.getType3Id()).getType3Name());
        return vo;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getPageCount(HttpSession session) {
        return goodsDao.queryGoodsByUserAll(((User) session.getAttribute("loginInfo")).getUserId()).size();
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getGoodsPageShow(Pages pages, HttpSession session) {
        List<Goods> loginInfo = goodsDao.queryGoodsByUserAndPage(pages.getPage(), pages.getLimit(),
                ((User) session.getAttribute(
                        "loginInfo")).getUserId());
        List<ResultGoodsVO> list = new ArrayList<>();
        Result<ResultGoodsVO> result = new Result<>();
        for (Goods goods : loginInfo) {
            ResultGoodsVO vo = new ResultGoodsVO();
            BeanUtils.copyProperties(goods, vo);
            vo.setType1Id(type1Dao.queryType1ById(goods.getType1Id()).getType1Name())
                    .setType2Id(type2Dao.queryBytype2d(goods.getType2Id()).getType2Name())
                    .setType3Id(type3Dao.queryBytype3d(goods.getType3Id()).getType3Name());
            list.add(vo);
        }
        result.setData(list)
                .setMsg("success")
                .setCode(0)
                .setCount(goodsDao.queryGoodsByUserAll(((User) session.getAttribute("loginInfo")).getUserId()).size());
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Integer getPageCountToiIndex() {
        return goodsDao.getPageCountToiIndex().size();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getGoodsShowToIndex(Pages pages, HttpSession session) {
        List<Goods> loginInfo = goodsDao.queryGoodsByUserAndPageToIndex(pages.getPage(), pages.getLimit());
        List<ResultGoodsVO> list = new ArrayList<>();
        Result<ResultGoodsVO> result = new Result<>();
        for (Goods goods : loginInfo) {
            ResultGoodsVO vo = new ResultGoodsVO();
            BeanUtils.copyProperties(goods, vo);
            vo.setType1Id(type1Dao.queryType1ById(goods.getType1Id()).getType1Name())
                    .setType2Id(type2Dao.queryBytype2d(goods.getType2Id()).getType2Name())
                    .setType3Id(type3Dao.queryBytype3d(goods.getType3Id()).getType3Name());
            list.add(vo);
        }
        result.setData(list)
                .setMsg("success")
                .setCode(0)
                .setCount(goodsDao.getPageCountToiIndex().size());
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getGoodsShowById(Integer id) {
        Goods loginInfo = goodsDao.queryById(id);
        ShowGoodsVO vo = new ShowGoodsVO();
        BeanUtils.copyProperties(loginInfo, vo);
        vo.setType1Id(type1Dao.queryType1ById(loginInfo.getType1Id()).getType1Name())
                .setType2Id(type2Dao.queryBytype2d(loginInfo.getType2Id()).getType2Name())
                .setType3Id(type3Dao.queryBytype3d(loginInfo.getType3Id()).getType3Name())
                .setUserId(userDao.queryUserId(loginInfo.getUserId()).getUserName());
        return vo;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object seacherInfo(String text, Pages pages, HttpSession session) {
        List<Goods> loginInfo = goodsDao.seacherInfo(pages.getPage(), pages.getLimit(), text);
        List<ResultGoodsVO> list = new ArrayList<>();
        Result<ResultGoodsVO> result = new Result<>();
        for (Goods goods : loginInfo) {
            ResultGoodsVO vo = new ResultGoodsVO();
            BeanUtils.copyProperties(goods, vo);
            vo.setType1Id(type1Dao.queryType1ById(goods.getType1Id()).getType1Name())
                    .setType2Id(type2Dao.queryBytype2d(goods.getType2Id()).getType2Name())
                    .setType3Id(type3Dao.queryBytype3d(goods.getType3Id()).getType3Name());
            list.add(vo);
        }
        result.setData(list)
                .setMsg("success")
                .setCode(0)
                .setCount(goodsDao.seacherInfoAll(text).size());
        System.out.println("result = " + result);
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Integer seacherInfoCount(String text) {
        return goodsDao.seacherInfoAll(text).size();
    }
}

